package com.lenovo.leos.filebrowser.engine.operation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.Toast;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.engine.util.FileUtil;
import com.lenovo.leos.filebrowser.engine.util.Log;

class SetWallpaperThread extends Thread {
	private static final String TAG = "SetWallpaperThread";
	
    private final Handler mHandler;
    private final Context mContext;
    private final File mFile;

	public SetWallpaperThread(Context context, File file, Handler handler) {
		mHandler = handler;
		mContext = context;
		mFile = file;
	}
	
	private Bitmap stretchThumbNail(Bitmap orig, int w, int h) {
		if (null == orig)
			return null;
		else {
			int width = orig.getWidth();
			int height = orig.getHeight();

			Matrix matrix = new Matrix();

//			float factor = (float) 100 / Math.max(width, height);
			float scaleWidth = ((float) w / width);
			float scaleHeight = ((float) h / height);
		    matrix.postScale(scaleWidth, scaleHeight);
//			matrix.postScale(factor, factor);
			
			Bitmap newbmp = Bitmap.createBitmap(orig, 0, 0, width, height,
					matrix, true);

			return newbmp;
		}
	}

    @Override
	public void run() {
		try {
			InputStream s = new FileInputStream(mFile);
			Bitmap bitmap = BitmapFactory.decodeStream(s);
			if (bitmap == null) {
				Log.e(TAG, "can not get bitmap from file");
				mHandler.post(new Runnable(){
					//@Override
					public void run() {
						Toast.makeText(mContext,
								R.string.msg_setwallpaperfailed,
								Toast.LENGTH_SHORT).show();
						// TODO Auto-generated method stub
					}	
				});
			} else {
				Exception err = null;
				try {
					WallpaperManager mwpManager = WallpaperManager.getInstance(mContext);
					
					int h = mwpManager.getDesiredMinimumHeight();
					int w = mwpManager.getDesiredMinimumWidth();
					
					Log.i(TAG, "the height is " + String.valueOf(h));
					Log.i(TAG, "the width is " + String.valueOf(w));
										
					Log.i(TAG, "the width is " + bitmap.getWidth());
					Log.i(TAG, "the height is " + bitmap.getHeight());

					WallpaperManager.getInstance(mContext).setBitmap(bitmap);
					
					// [backup] below code is used to stretch the bitmap to desired size.
//					Bitmap newbp = stretchThumbNail(bitmap, w, h);					
//					WallpaperManager.getInstance(mContext).setBitmap(newbp);
					

				} catch (IOException e) {
					err = e;
					Log.e(TAG, "can not set wallpaper");
					mHandler.post(new Runnable(){
						//@Override
						public void run() {
							Toast.makeText(mContext,
									R.string.msg_setwallpaperfailed,
									Toast.LENGTH_SHORT).show();
							// TODO Auto-generated method stub
						}	
					});
				} finally {
					// mFile.delete();
				}
				
				if (null == err){
					mHandler.post(new Runnable(){
						//@Override
						public void run() {
							Toast.makeText(mContext,
									R.string.msg_setwallpaperok,
									Toast.LENGTH_SHORT).show();
							// TODO Auto-generated method stub
						}	
					});					
				}
			}
		} catch (FileNotFoundException ex) {
			Log.e(TAG, "failed to set wallpaper from file : "
					+ mFile.getAbsolutePath());
		}
	}
}

public class FileApply extends Object {
	private static final String TAG = "FileApply";
	
	public static void setAsWallpaper(Activity activity, File file, Handler handler) {
		Log.i(TAG, "==> applyAsWallpaper");
		
		if(null == file || !file.exists()){
			Log.e(TAG, "file is invalid in setAsWallpaper");
			return;
		}
		
		if (!FileUtil.isImageFile(activity, file)){
			Log.e(TAG, "file can be used as wallpaper");
			return;
		}
		
		new SetWallpaperThread(activity, file, handler).start();			
	}
}